package com.badshun.enemy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badshun.box2d.Box2DWorld;

public class BoneEnemy {
	public int hp;
	public int atk;
	public TextureRegion keyFrame;
	public float speed = 300;
	
	private OrthographicCamera camera;
	private Box2DWorld b2dWorld;
	private SpriteBatch batch;
	
	private Animation leftAnim, rightAnim, upAnim, downAnim;
	private Animation leftIdleAnim, rightIdleAnim, upIdleAnim, downIdleAnim;
	private Animation mainAnimation;
	
	private float playTime = 0.08f;
	
	public Direction direction = Direction.DOWN;
	public State state = State.IDLE;
	
	private float stateTime;
	
	public Vector2 position;
	public Body body;
	
	public BoneEnemy(OrthographicCamera camera, Box2DWorld b2dWorld) {
		this.camera = camera;
		this.b2dWorld = b2dWorld;
		this.batch = new SpriteBatch();
		
		Texture texture = new Texture(Gdx.files.internal("player/enemy.jpg"));
		
		TextureRegion[][] textureRegionses = TextureRegion.split(texture, texture.getWidth() / 4, texture.getHeight() / 4);
		
		leftAnim = new Animation(playTime, textureRegionses[1]);
		rightAnim = new Animation(playTime, textureRegionses[2]);
		upAnim = new Animation(playTime, textureRegionses[3]);
		downAnim = new Animation(playTime, textureRegionses[0]);
		
		leftAnim.setPlayMode(Animation.PlayMode.LOOP);
		rightAnim.setPlayMode(Animation.PlayMode.LOOP);
		upAnim.setPlayMode(Animation.PlayMode.LOOP);
		downAnim.setPlayMode(Animation.PlayMode.LOOP);
		
		leftIdleAnim = new Animation(playTime, textureRegionses[1][0]);
		rightIdleAnim = new Animation(playTime, textureRegionses[2][0]);
		upIdleAnim = new Animation(playTime, textureRegionses[3][0]);
		downIdleAnim = new Animation(playTime, textureRegionses[0][0]);
		
		leftIdleAnim.setPlayMode(Animation.PlayMode.LOOP);
		rightIdleAnim.setPlayMode(Animation.PlayMode.LOOP);
		upIdleAnim.setPlayMode(Animation.PlayMode.LOOP);
		downIdleAnim.setPlayMode(Animation.PlayMode.LOOP);
		
		keyFrame = textureRegionses[0][0];
		
		position = new Vector2((Gdx.graphics.getWidth() / 2 + keyFrame.getRegionWidth() / 2 + 200) / 2,(Gdx.graphics.getHeight() / 2 + keyFrame.getRegionHeight() / 2) / 2);
		
		body = createBody();
	}
	
	private void autoSetMainAnimation() {
		if(state == State.IDLE) {
			switch (direction) {
				case LEFT:
					mainAnimation = leftIdleAnim;
					break;
				case RIGHT:
					mainAnimation = rightIdleAnim;
					break;
				case UP:
					mainAnimation = upIdleAnim;
					break;
				case DOWN:
					mainAnimation = downIdleAnim;
					break;
			}
		} else if(state == State.WALK) {
			switch (direction) {
				case LEFT:
					mainAnimation = leftAnim;
					break;
				case RIGHT:
					mainAnimation = rightAnim;
					break;
				case UP:
					mainAnimation = upAnim;
					break;
				case DOWN:
					mainAnimation = downAnim;
					break;
			}
		}
	}
	
	private Body createBody() {
		BodyDef bDef = new BodyDef();
		bDef.position.set(position);
		bDef.type = BodyDef.BodyType.DynamicBody;
		
		Body body = b2dWorld.world.createBody(bDef);
		
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(keyFrame.getRegionWidth() / 2 - 10, keyFrame.getRegionHeight() / 2 - 10);
		
		FixtureDef fDef = new FixtureDef();
		fDef.shape = shape;
		fDef.filter.categoryBits = 8;
		fDef.filter.maskBits = 2 | 4;
		
		body.createFixture(fDef);
		
		return body;
	}
	
	public void move(float knobPercentX, float konbPercentY, State state, Direction direction) {
		this.state = state;
		this.direction = direction;
		
		if(body.getLinearVelocity().x < speed + 1 && body.getLinearVelocity().x > -speed - 1 && body.getLinearVelocity().y < speed + 1 && body.getLinearVelocity().y > -speed - 1) {
			if(knobPercentX == 0 && konbPercentY != 0) {
				body.applyLinearImpulse(new Vector2(-body.getLinearVelocity().x, konbPercentY * speed), body.getWorldCenter(), true);
			} else if(knobPercentX !=0 && konbPercentY == 0) {
				body.applyLinearImpulse(new Vector2(knobPercentX * speed, -body.getLinearVelocity().y), body.getWorldCenter(), true);
			} else if(knobPercentX == 0 && konbPercentY == 0) {
				body.applyLinearImpulse(new Vector2(-body.getLinearVelocity().x, -body.getLinearVelocity().y), body.getWorldCenter(), true);
			} else {
				body.applyLinearImpulse(new Vector2(knobPercentX * speed, konbPercentY * speed), body.getWorldCenter(), true);
			}
		}
	}
	
	public void render() {
		autoSetMainAnimation();
		
		position.set(body.getPosition().x - keyFrame.getRegionWidth() / 2, body.getPosition().y - keyFrame.getRegionHeight() / 2 + 10);
		
		keyFrame = mainAnimation.getKeyFrame(stateTime);
		
		batch.begin();
		batch.setProjectionMatrix(camera.combined);
		batch.draw(keyFrame, position.x, position.y);
		batch.end();
		
		stateTime += Gdx.graphics.getDeltaTime();
	}
	
	public enum Direction {
		UP, DOWN, LEFT, RIGHT
	}
	
	public enum State {
		WALK, IDLE, ATK
	}
}






























